﻿using CheemsControl.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CheemsControl.Models.MessageTimeline
{
    /// <summary>
    /// 消息数据
    /// </summary>
    public class MessageDatas : INotifyPropertyChanged
    {
        private ObservableCollection<SingleMessage> _messages;

        public ObservableCollection<SingleMessage> Messages
        {
            get => _messages;
            set
            {
                if (_messages != value)
                {
                    if (_messages != null)
                        _messages.CollectionChanged -= OnMessagesChanged;

                    _messages = value;

                    if (_messages != null)
                        _messages.CollectionChanged += OnMessagesChanged;

                    OnPropertyChanged(nameof(Messages));
                }
            }
        }

        public MessageDatas()
        {
            Messages = new ObservableCollection<SingleMessage>();
            Messages.CollectionChanged += OnMessagesChanged;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnMessagesChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // 集合发生变更时通知外部
            OnPropertyChanged(nameof(Messages));
        }

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public void AddMessage(SingleMessage message)
        {
            Messages.Add(message); // 此时会触发 CollectionChanged
        }

        public void RemoveMessage(SingleMessage message)
        {
            if (Messages.Contains(message))
            {
                Messages.Remove(message); // 此时会触发 CollectionChanged
            }
        }

        public void RemoveMessageAt(int index)
        {
            if (index >= 0 && index < Messages.Count)
            {
                Messages.RemoveAt(index); // 此时会触发 CollectionChanged
            }
        }

        public void UpdateMessage(SingleMessage oldMessage, SingleMessage newMessage)
        {
            int index = Messages.IndexOf(oldMessage);
            if (index >= 0)
            {
                Messages[index] = newMessage; // 此时会触发 CollectionChanged
            }
        }

        public SingleMessage FindMessage(Func<SingleMessage, bool> predicate)
        {
            return Messages.FirstOrDefault(predicate);
        }

        public IEnumerable<SingleMessage> FindMessages(Func<SingleMessage, bool> predicate)
        {
            return Messages.Where(predicate);
        }

        public override bool Equals(object obj)
        {
            if (obj is MessageDatas other)
            {
                // 首先，检查 Messages 集合的长度是否相同
                if (Messages.Count != other.Messages.Count)
                    return false;

                // 然后，逐一比较 Messages 集合中的每个 SingleMessage 对象
                for (int i = 0; i < Messages.Count; i++)
                {
                    if (!Messages[i].Equals(other.Messages[i]))
                        return false;
                }

                return true;
            }

            return false;
        }

        // 重写 GetHashCode 以确保它与 Equals 配对使用
        public override int GetHashCode()
        {
            int hashCode = 0;
            foreach (var message in Messages)
            {
                hashCode ^= message.GetHashCode();
            }
            return hashCode;
        }

        public MessageDatas Clone()
        {
            var clonedMessageDatas = new MessageDatas();

            // 克隆消息集合
            foreach (var message in Messages)
            {
                clonedMessageDatas.Messages.Add(new SingleMessage
                {
                    time = message.time,
                    tips = message.tips,
                    type = message.type
                });
            }

            return clonedMessageDatas;
        }

        /// <summary>
        /// 获取最早的消息
        /// </summary>
        /// <returns></returns>
        public SingleMessage GetEarliestMessage()
        {
            return Messages.OrderBy(m => m.time).FirstOrDefault();
        }

        /// <summary>
        /// 获取最晚的消息
        /// </summary>
        /// <returns></returns>
        public SingleMessage GetLatestMessage()
        {
            return Messages.OrderByDescending(m => m.time).FirstOrDefault();
        }
    }


}
